package com.miracle9.game.thread;

import com.miracle9.game.entity.*;
import com.miracle9.game.enums.GameTypeEnums;
import com.miracle9.game.service.AdminService;
import com.miracle9.game.service.UserService;
import com.miracle9.game.service.UserTopService;
import com.miracle9.game.util.GameParameterAssist;
import com.miracle9.game.util.MyUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("initDataThread")
public class InitDataThread extends Thread {

    private final Logger logger = Logger.getLogger(InitDataThread.class);
    @Autowired
    private UserTopService userTopService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;

    @PostConstruct
    public void initDataThreadStart() {
        start();
    }

    public void run() {
        try {
            hallStart();
        } catch (Exception e) {
            this.logger.error("", e);
        }
    }

    public void hallStart() {
        for (Integer gameType : GameTypeEnums.gameTypes()) {
            List<UserTop> userTops = this.userTopService.topList(gameType, 1);
            GameParameterAssist.aK.put(gameType, userTops);
        }
        List<Admin> admins = this.adminService.getAdminList();
        for (Admin admin : admins) {
            AdminLog log = this.adminService.getLastLog(admin);
            if (log != null) {
                GameParameterAssist.aL.put(admin.getUsername(), log);
            }
        }
        List<User> users = this.userService.getUserList();
        List<UserWarning> userWarnings = this.userService.getUserWarning();
        List<UserWarningBak> userWarningBaks = this.userService.getUserWarningBak();
        UserWarning userWarning = new UserWarning();
        UserWarningBak userWarningBak = new UserWarningBak();
        if (userWarnings.size() == 0) {
            for (User user : users) {
                userWarning.setUserId(user.getId());
                userWarning.setGameGold(user.getGameGold());
                userWarningBak.setUserId(user.getId());
                userWarningBak.setGameGold(user.getGameGold());
                userWarningBak.setDate(MyUtil.dateFormatYMD_HMS_Sting(new Date()));
                this.userService.addUserWarning(userWarning);
                this.userService.addUserWarningBak(userWarningBak);
            }
        } else {
            List<Integer> list1 = new ArrayList<>();
            List<Integer> list2 = new ArrayList<>();
            for (User user : users) {
                list1.add(user.getId());
            }
            for (UserWarning userWarning1 : userWarnings) {
                list2.add(userWarning1.getUserId());
            }
            for (UserWarning warning : userWarnings) {
                list2.add(warning.getUserId());
            }
            if (list1.size() < list2.size()) {
                list2.removeAll(list1);
                for (Integer userId : list2) {
                    if (userWarnings.size() == userWarningBaks.size()) {
                        this.userService.deleteWarning(userId);
                    } else {
                        this.logger.info("预警机制：主表和从表数据长度不一致");
                    }
                }
            } else if (list1.size() > list2.size()) {
                list1.removeAll(list2);
                for (Integer userId : list1) {
                    if (userWarnings.size() == userWarningBaks.size()) {
                        User newUser = this.userService.getUser(userId);
                        userWarning.setUserId(newUser.getId());
                        userWarning.setGameGold(newUser.getGameGold());
                        userWarningBak.setUserId(newUser.getId());
                        userWarningBak.setGameGold(newUser.getGameGold());
                        userWarningBak.setDate(MyUtil.dateFormatYMD_HMS_Sting(new Date()));
                        this.userService.addUserWarning(userWarning);
                        this.userService.addUserWarningBak(userWarningBak);
                    } else {
                        this.logger.info("预警机制：主表和从表数据长度不一致");
                    }
                }
            }
        }
        this.logger.info("*****************************************大厅服务器启动成功");
    }

}
